package com.linq.cool.system.service.impl;

import com.linq.cool.api.domain.SysUser;
import com.linq.cool.system.service.SysMenuService;
import com.linq.cool.system.service.SysPermissionService;
import com.linq.cool.system.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author: yqlin
 * @Date: 2021/1/19 02:42
 * @Description:
 * @Version: 1.0.0
 */
@Service
public class SysPermissionServiceImpl implements SysPermissionService {
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysMenuService sysMenuService;

    @Override
    public Set<String> listRolePermissionByUserId(Long userId) {
        Set<String> roleSet = new HashSet<>();
        // 管理员拥有所有权限
        if (SysUser.isAdmin(userId)) {
            roleSet.add("admin");
        } else {
            roleSet.addAll(sysRoleService.listRolePermissionByUserId(userId));
        }
        return roleSet;
    }

    @Override
    public Set<String> listMenuPermissionByUserId(Long userId) {
        Set<String> menuSet = new HashSet<>();
        if (SysUser.isAdmin(userId)) {
            menuSet.add("*:*:*");
        } else {
            menuSet.addAll(sysMenuService.listMenuPermissionByUserId(userId));
        }
        return menuSet;
    }
}
